package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

//菜品管理
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishControler {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    //菜品新增
    @PostMapping
    @ApiOperation("菜品新增")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("菜品新增：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

    //菜品分页查询,传入条件：名称，分类，状态，页面，页大小
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /*菜品批量删除
    起售中的菜品不能删除，被套餐关联的菜品不能删除，关联的口味也要删除
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){//RequestParam: 指定请求参数的名称，用于匹配请求参数的值
        log.info("菜品的批量删除");
        dishService.deleteBatch(ids);
        //清理缓存数据
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /*
    菜品的修改,起售和停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售和停售")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("起售和停售：{}",id);
        dishService.startOrStop(status,id);
        //清理缓存数据,修改比较复杂，可以通过清理所有菜品缓存数据
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /*
    菜品的修改
     */

    @PutMapping
    @ApiOperation("菜品的修改")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("菜品的修改：{}",dishDTO);
        dishService.update(dishDTO);
        //清理缓存数据,修改比较复杂，可以通过清理所有菜品缓存数据
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /*
    菜品的查询根据id
     */
    @GetMapping("/{id}")
    @ApiOperation("菜品的查询根据id")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("菜品的查询根据id：{}",id);
        DishVO DishVO = dishService.getById(id);
        return Result.success(DishVO);
    }

    /*
    根据分类查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类查询菜品")
    public Result list(Long categoryId){
        log.info("根据分类查询菜品：{}",categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}
